package com.freechurch.view.api;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.TreeSet;

import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import com.freechurch.app.Application;
import com.freechurch.view.Main;
import com.freechurch.view.membro.MembroFormList;

public class MenuBar extends JMenuBar implements InternalFrameListener {

	private MembroFormList membroFormList;

	private static final long serialVersionUID = 8073188569923827618L;
	private JMenu arquivo, cadastro, relatorios;
	private JMenuItem sair, membros, aniversariantesDoMes;
	private static MenuBar innerInstance = null;
	private TreeSet<JInternalFrame> frames = new TreeSet<JInternalFrame>();

	private MenuBar() {
		this.add(getArquivo());
		this.add(getRelatorios());
	}

	public static MenuBar getInstance() {
		if (innerInstance == null) {
			innerInstance = new MenuBar();
		}
		return innerInstance;
	}

	public JMenu getArquivo() {
		if (arquivo == null) {
			arquivo = new JMenu("Arquivo");
			arquivo.add(getCadastro());
			arquivo.add(getSair());
		}
		return arquivo;
	}

	public JMenu getCadastro() {
		if (cadastro == null) {
			cadastro = new JMenu("Cadastro");
			cadastro.add(getMembros());
		}
		return cadastro;
	}

	public JMenu getRelatorios() {
		if (relatorios == null) {
			relatorios = new JMenu("Relatorios");
			relatorios.add(getAniversariantesDoMes());
		}
		return relatorios;
	}

	public void setRelatorios(JMenu relatorios) {
		this.relatorios = relatorios;
	}

	public JMenuItem getSair() {
		if (sair == null) {
			sair = new JMenuItem("Sair");
			sair.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent arg0) {
					Application.getInstance().exit();
				}
			});
		}
		return sair;
	}

	public void setSair(JMenuItem sair) {
		this.sair = sair;
	}

	public JMenuItem getMembros() {
		if (membros == null) {
			membros = new JMenuItem("Membros");
			membros.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					getMembroFormList().addInternalFrameListener(getInstance());
					Main.getInstance().addFrame(getMembroFormList());
				}
			});
		}
		return membros;
	}

	public JMenuItem getAniversariantesDoMes() {
		if (aniversariantesDoMes == null) {
			aniversariantesDoMes = new JMenuItem("Aniversariantes do mes");
		}
		return aniversariantesDoMes;
	}

	public MembroFormList getMembroFormList() {
		if (membroFormList == null) {
			membroFormList = new MembroFormList();
			frames.add(membroFormList);
		}
		return membroFormList;
	}

	public void internalFrameActivated(InternalFrameEvent arg0) {

	}

	public void internalFrameClosed(InternalFrameEvent frame) {
		JInternalFrame _frame = frame.getInternalFrame();
		if (frames.contains(_frame))
			frames.remove(_frame);
		Main.getInstance().removeFrame(frame.getInternalFrame());
		frame = null;
	}

	public void internalFrameClosing(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void internalFrameDeactivated(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void internalFrameDeiconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void internalFrameIconified(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void internalFrameOpened(InternalFrameEvent arg0) {
		// TODO Auto-generated method stub

	}

}
